﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using LastStand.gameContent;

namespace LastStand
{
	public partial class GamePage : PhoneApplicationPage
	{
        UIElementRenderer elementRenderer;
		ContentManager contentManager;
		GameTimer timer;
		SpriteBatch spriteBatch;

        //allows us to focus xaml controls on the selected structure.
        Structure selectedFriendly;

        //gamecore executes the game load update and draw
        GameCore gameCore;

		public GamePage()
		{
			InitializeComponent();

            LayoutUpdated += new EventHandler(GamePage_LayoutUpdated);

			// Get the content manager from the application
			contentManager = (Application.Current as App).Content;

			// Create a timer for this page
			timer = new GameTimer();
			timer.UpdateInterval = TimeSpan.FromTicks(333333);
			timer.Update += OnUpdate;
			timer.Draw += OnDraw;

            //construct the gameCore
            gameCore = new GameCore(grid);
		}

        void GamePage_LayoutUpdated(object sender, EventArgs e)
        {
            // Create the UIElementRenderer to draw the XAML page to a texture.

            // Check for 0 because when we navigate away the LayoutUpdate event
            // is raised but ActualWidth and ActualHeight will be 0 in that case.
            if ((ActualWidth > 0) && (ActualHeight > 0))
            {
                SharedGraphicsDeviceManager.Current.PreferredBackBufferWidth = (int)ActualWidth;
                SharedGraphicsDeviceManager.Current.PreferredBackBufferHeight = (int)ActualHeight;
            }

            if (null == elementRenderer)
            {
                elementRenderer = new UIElementRenderer(this, (int)ActualHeight, (int)ActualWidth);
            }
        }

		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			// Set the sharing mode of the graphics device to turn on XNA rendering
			SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

			// Create a new SpriteBatch, which can be used to draw textures.
			spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

			// TODO: use this.content to load your game content here
            gameCore.LoadContent(contentManager);

			// Start the timer
			timer.Start();

			//set the game state to playing
			gameCore.gameState = GameCore.GameState.PLAYING;

			base.OnNavigatedTo(e);
		}

		protected override void OnNavigatedFrom(NavigationEventArgs e)
		{
			// Stop the timer
			timer.Stop();

			//pause the game state
			gameCore.gameState = GameCore.GameState.PAUSED;

			// Set the sharing mode of the graphics device to turn off XNA rendering
			SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

			base.OnNavigatedFrom(e);
		}

		/// <summary>
		/// Allows the page to run logic such as updating the world,
		/// checking for collisions, gathering input, and playing audio.
		/// </summary>
		private void OnUpdate(object sender, GameTimerEventArgs e)
		{
			// TODO: Add your update logic here
            gameCore.Update(e);
		}

		/// <summary>
		/// Allows the page to draw itself.
		/// </summary>
		private void OnDraw(object sender, GameTimerEventArgs e)
		{
			SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

			// TODO: Add your drawing code here
            elementRenderer.Render();

            //begins the draw process
            spriteBatch.Begin();

            //draws all gamecotent from the gamecore
            gameCore.Draw(spriteBatch);

            //draws xaml controls.
            spriteBatch.Draw(elementRenderer.Texture, Vector2.Zero, Color.White);

            //ends the draw process
            spriteBatch.End();
		}
	}
}